home *** CD-ROM | disk | FTP | other *** search
/ Belgian Amiga Club - ADF Collection / BS1 part 19.zip / BS1 part 19 / AmigaLibDisk 156.adf / Blocks2 / Blocks2.c < prev    next >
C/C++ Source or Header  |  1988-10-01  |  6KB  |  297 lines

  1. /*
  2. *    A Simple program to display colorful blocks on a custom screen.
  3. *    Compiles with no errors under Lattice 3.10, However, it passes ints to
  4. *    some subroutines and does structure assignments, so manx users will
  5. *    probably have to do a little work.
  6. *            Linesdemo original by    Paul Jatkowski
  7. *    Block rendering, PAL tests, windowsize checking by Gary Walker        
  8. */
  9. #include <exec/types.h>
  10. #include <intuition/intuition.h>
  11. #include <graphics/gfxbase.h>
  12. #include <graphics/gfx.h>
  13. #include <graphics/gfxmacros.h>
  14. #include <graphics/view.h>
  15. extern struct IntuitionBase *IntuitionBase;
  16. struct GfxBase *GfxBase;
  17.  
  18. struct Point {
  19.     int x;
  20.     int y;
  21. };
  22.  
  23. #define Q 5
  24. #define N (Q*12)    /* N is the number of blocks displayed on the screen */
  25.             /* change the value of Q to change the number of    */
  26.             /* blocks so that it is evenly divisible by the      */
  27.             /* number of color registers used            */
  28.  
  29. int    curcolor;
  30. Point    from[N], to[N];
  31. Point    dfrom, dto;
  32. long    i,j;
  33. int    minx,maxx,miny,maxy;
  34.  
  35.  
  36. struct TextAttr MyFont = {
  37.    "topaz.font",           /* Font Name */
  38.    TOPAZ_SIXTY,            /* Font Height */
  39.    FS_NORMAL,              /* Style */
  40.    FPF_ROMFONT             /* Preferences */
  41. };
  42.  
  43.  
  44. struct NewScreen NewScreen = {
  45.    0,             /* left edge */
  46.    0,             /* top edge */
  47.    640,           /* Width (high res) */
  48.    512,           /* Height (interlace + PAL overscan) */
  49.    4,             /* Depth */
  50.    0,1,           /* Detail and Block Pen Specification */
  51.    HIRES|LACE,      /* Hires Interlaced screen */
  52.    CUSTOMSCREEN,  /* The Screen Type */
  53.    &MyFont,       /* our font */
  54.    "My Own Screen", /* title */
  55.    NULL,          /* no special gadgets */
  56.    NULL           /* no special custom BitMap */
  57. };
  58.  
  59.  
  60. struct   Window   *Window;
  61. struct   Screen   *Screen;
  62. struct   RastPort  *RP;
  63.  
  64. main()
  65. {
  66.  
  67.     struct   NewWindow NewWindow;
  68.     struct   ViewPort  *VP;
  69.     int    notdone = 1;
  70.     struct    IntuiMessage *msg , *GetMsg();
  71.     int    my_rgbi,my_rgb[3];
  72.     int    inc,colorok;
  73.     int    lc = -1;
  74.     int     Ht;
  75.  
  76.     /* open intuition library */
  77.     IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library",0);
  78.     if (IntuitionBase == NULL)
  79.         exit(FALSE);
  80.     GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",0);
  81.     if (GfxBase == NULL)
  82.         exit (FALSE);
  83.       if ((*GfxBase).DisplayFlags & PAL)
  84.         Ht = 512 ;
  85.         
  86.       else  Ht = 400 ;
  87.         
  88.  
  89.     if ((Screen = (struct Screen *)OpenScreen(&NewScreen)) == NULL)
  90.         exit(FALSE);
  91.     
  92.     /* set up new window structure */
  93.     NewWindow.LeftEdge = 0;
  94.     NewWindow.TopEdge = 0;
  95.     NewWindow.Width = 640;
  96.     NewWindow.Height = Ht;
  97.     NewWindow.DetailPen = 0;
  98.     NewWindow.BlockPen = 1;
  99.     NewWindow.Title = "A Simple Window";
  100.     NewWindow.Flags = WINDOWCLOSE | SMART_REFRESH |
  101.                 WINDOWDRAG | WINDOWSIZING;
  102.     NewWindow.IDCMPFlags = CLOSEWINDOW | NEWSIZE ;
  103.     NewWindow.Type = CUSTOMSCREEN;
  104.     NewWindow.FirstGadget = NULL;
  105.     NewWindow.CheckMark = NULL;
  106.     NewWindow.Screen = Screen;
  107.     NewWindow.BitMap = NULL;
  108.     NewWindow.MinWidth = 160;
  109.     NewWindow.MinHeight = 100;
  110.     NewWindow.MaxWidth = 640;
  111.     NewWindow.MaxHeight = Ht;
  112.  
  113.     /* try to open the window */
  114.     if ((Window = (struct Window *)OpenWindow(&NewWindow)) == NULL)
  115.         exit(FALSE);
  116.    
  117.     RP = Window->RPort;
  118.     VP = (struct ViewPort *) ViewPortAddress(Window);
  119.    
  120.  
  121.     SetAPen(RP,14);
  122.     SetBPen(RP,0);
  123.     SetWindowTitles(Window,"Blocks V2.0 - by Gary Walker","");
  124.     SetDrMd(RP,JAM1);
  125.     SetAPen(RP,15);
  126.  
  127.     init();
  128.     SetRGB4(VP,0,0,0,0);     
  129.     SetRGB4(VP,1,1,6,12);     
  130.     SetRGB4(VP,2,5,0,8);  
  131.     SetRGB4(VP,3,0,8,5);     
  132.  
  133.     my_rgb[0] = rand() % 16;
  134.     my_rgb[1] = rand() % 16;
  135.     my_rgb[2] = rand() % 16;
  136.     SetRGB4(VP,curcolor,my_rgb[0],my_rgb[1],my_rgb[2]);
  137.     SetDrMd(RP,JAM1);            
  138.     while(notdone)
  139.     {
  140.         j = i;
  141.         if (++i >= N)
  142.             i = 0;
  143.  
  144.         SetAPen(RP,0);            /* erase old block */
  145.         RectFill(RP, from[i].x, from[i].y, from[i].x + 20, from[i].y + 20);
  146.         from[i] = from[j];        /* structure assignment */
  147.         mv_point(&from[i], &dfrom);
  148.         to[i] = to[j];            /* structure assignment */
  149.         mv_point(&to[i], &dto);
  150.  
  151.         SetAPen(RP,curcolor);        /* draw a new block */
  152.         RectFill(RP, from[i].x, from[i].y, from[i].x + 20, from[i].y + 20);
  153.                 
  154.         if ( (i % Q) == 0)
  155.         {
  156.             if (++curcolor > 15)
  157.                 curcolor = 4;
  158.             colorok = 0;
  159.             while (!colorok)
  160.             {
  161.                 inc = 1;
  162.                 if (rand() & 8 )
  163.                     inc = -1;
  164.                 my_rgbi = rand() % 3;
  165.                 inc += my_rgb[my_rgbi];
  166.                 /* make sure that the color register
  167.                    doesn't wrap and we don't change the same
  168.                    color twice in a row
  169.                 */
  170.                 if (inc <= 15 && inc >= 0 && lc != my_rgbi)
  171.                 {
  172.                     my_rgb[my_rgbi] = inc;
  173.                     colorok++;
  174.                     lc = my_rgbi;
  175.                 }
  176.             }
  177.             SetRGB4(VP,curcolor,my_rgb[0],my_rgb[1],my_rgb[2]);
  178.         }
  179.         if ( (rand() % 20) == 1)
  180.         {
  181.             if (rand() & 2)
  182.             {
  183.                 newdelta(&to[i],&dto);
  184.             }
  185.             else
  186.             {
  187.                 newdelta(&from[i],&dfrom);
  188.             }
  189.         }
  190.  
  191.         while ((msg = GetMsg(Window->UserPort)) != 0)
  192.         {
  193.             switch(msg->Class)
  194.             {
  195.  
  196.             case CLOSEWINDOW:    /* that's all folks */
  197.                 notdone = 0;
  198.                 ReplyMsg(msg);
  199.                 continue;
  200.             case NEWSIZE:
  201.                 ReplyMsg(msg);            
  202.                 init();
  203.                 break;            
  204.             default:
  205.                 ReplyMsg(msg);
  206.             }
  207.         }
  208.     }
  209.     
  210.  
  211.     /* close the window and  exit */
  212.     CloseWindow(Window);
  213.     CloseScreen(Screen);
  214.     exit(TRUE);
  215. }
  216.  
  217.  
  218. init()
  219. {
  220.     ULONG    seconds,micros;
  221.     
  222.     miny = Window->BorderTop;
  223.     maxy = (Window->Height - Window->BorderBottom) - 20;
  224.     minx = Window->BorderLeft;
  225.     maxx = (Window->Width - Window->BorderRight) - 5;
  226.  
  227.     for (j=0 ; j <N ; j++)
  228.     {
  229.         from[j].x = 20; from[j].y = 20;
  230.         to[j].x = 0; to[j].y = 0;
  231.     }
  232.     /* attempt to ramdomize the random number generator */
  233.     CurrentTime(&seconds,µs);
  234.     srand(micros);
  235.     
  236.     from[0].x = range_rand(minx,maxx);
  237.     from[0].y = range_rand(miny,maxy);
  238.     from[1] = from[0];
  239.     
  240.     to[0].x = range_rand(minx,maxx);
  241.     to[0].y = range_rand(miny,maxy);
  242.     to[1] = to[0];
  243.  
  244.     newdelta(&from[0],&dfrom);
  245.     newdelta(&to[0],&dto);    
  246.     i = 0;
  247.     curcolor = 4;
  248.     
  249.     /* clear the screen */
  250.     SetAPen(RP,0);
  251.     SetOPen(RP,0);
  252.     RectFill(RP, minx, miny, maxx + 5, maxy + 20);
  253. }
  254.  
  255. range_rand(minv,maxv)
  256. {
  257.     register int i1;
  258.     
  259.     i1 = minv + (rand() % (maxv - minv));
  260. }
  261.  
  262. mv_point(p,dp)
  263. register Point *p, *dp;
  264. {
  265.     if ((p->x += dp->x) > maxx || p->x < minx)
  266.     {
  267.         dp->x = -dp->x;
  268.         p->x += dp->x;
  269.     }
  270.     if ((p->y += dp->y) > maxy || p->y < miny)
  271.     {
  272.         dp->y = -dp->y;
  273.         p->y += dp->y;
  274.     }
  275. }
  276. newdelta(p,dp)
  277. register Point *p, *dp;
  278. {
  279.     for (dp->x = getdelta() ;
  280.             ((p->x + dp->x) > maxx) || ((p->x + dp->x) < minx) ;
  281.             dp->x = getdelta())
  282.         ;
  283.     for (dp->y = getdelta() ;
  284.             ((p->y + dp->y) > maxy) || ((p->y + dp->y) < miny) ;
  285.             dp->y = getdelta() )
  286.         ;
  287. }
  288.  
  289. getdelta()
  290. {
  291.     register int x;
  292.  
  293.     x = (8 - (rand() % 16));
  294. }
  295.  
  296.  
  297.